#!/usr/bin/python
import serial
import re
from datetime import datetime
from config import config

# set this to your serial port
PORT = config['port']
class SolarReader:


	# format is' P:0 T:16.206 L:232'
	def getEnergy(self):
		ser = serial.Serial(PORT, 9600, timeout=5)
		ser.flushOutput()
		ser.write("\r")
		ser.write('MEASENGY?\r')
		ser.write('MEASENGY?\r')
		ser.flushInput()

		line = ''
		byte = 0
		while byte != '\r':
			byte = ser.read()
			line = line + byte
		ser.close()
		m = re.match(".*P:(.*) T:(.*) L:(.*)\r",line)
		return (int(m.group(1)),float(m.group(2)),int(m.group(3)))

	

if __name__ == "__main__":
	reader = SolarReader()
	watts, kwh, kwh_lifetime = reader.getEnergy()
	print "%d watts, %f kwh today, %d kwh lifetime" % (watts, kwh, kwh_lifetime)
